package com.ceasa.customviews;

import java.net.MalformedURLException;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ceasa.R;
import com.ceasa.bean.Produto;
import com.ceasa.classes.ImageThreadLoader;

//import android.widget.ImageView;

public class CustomAdapterProduct extends ArrayAdapter<Produto> {

	/*
	 * Used to instantiate layout XML file into its corresponding View objects
	 */
	private LayoutInflater inflater;

	/*
	 * each list item layout ID
	 */
	private final int resourceId;
	private final Context context;

	private final static String TAG = "CustomAdapterProduct";

	private ImageThreadLoader imageLoader = new ImageThreadLoader();

	public CustomAdapterProduct(Context context, int resource,
			List<Produto> objects) {
		super(context, resource, objects);
		// this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.resourceId = resource;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		final ImageView image;
		TextView name;
		TextView description;
		Button btnBuyTicket;
		view = inflater.inflate(resourceId, parent, false);
		try {
			image = (ImageView) view.findViewById(R.id.imgProductFolder);
			name = (TextView) view.findViewById(R.id.lblProductName);
			description = (TextView) view
					.findViewById(R.id.lblProductDescription);
			btnBuyTicket = (Button) view.findViewById(R.id.btnBuy);

		} catch (ClassCastException e) {
			Log.e(TAG,
					"Your layout must provide an image and a text view with ID's icon and text.",
					e);
			throw e;
		}
		final Produto produto = getItem(position);
		Bitmap cachedImage = null;
		produto.setThumbnail("http://www.guidebar.com.br/guidebar/uploads/produto/"
				+ produto.getId() + ".jpg");
		try {
			cachedImage = imageLoader.loadImage(produto.getThumbnail(),
					new ImageThreadLoader.ImageLoadedListener() {
						public void imageLoaded(Bitmap imageBitmap) {
							image.setImageBitmap(imageBitmap);
							notifyDataSetChanged();
						}
					});
		} catch (MalformedURLException e) {
			Log.e(TAG, "Bad remote image URL: " + produto.getThumbnail(), e);
		}
		if (cachedImage != null) {
			image.setImageBitmap(cachedImage);
		}

		name.setText(produto.getNome());
		description.setText(produto.getDescricao());
		Typeface tf = Typeface.createFromAsset(context.getAssets(),
				"fonts/Gotham-Book.otf");
		name.setTypeface(tf);
		description.setTypeface(tf);
		btnBuyTicket.setTypeface(tf);
		btnBuyTicket.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setEnabled(false);
				//Intent i = new Intent(context, PurchaseActivity.class);
//				i.putExtra("id_evento", evento.getId());
//				i.putExtra("id_promotor", evento.getPromotor().getId());
//				context.startActivity(i);
				// v.setEnabled(true);
			}
		});
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setEnabled(false);
				v.setBackgroundColor(context.getResources().getColor(
						R.color.light_grey));
//				Intent ITelaVisualizacao = new Intent(context,
//						ViewProductActivity.class);
//				ITelaVisualizacao.putExtra("id", evento.getId());
//				((ListProductsActivity) context).startActivityForResult(
//						ITelaVisualizacao, 0);
				// v.setEnabled(true);
			}
		});

		return view;
	}

	public String getDateForPresenting(String str) {
		if (str != null) {
			String[] date = str.split("-");
			String date2 = date[2] + "/" + date[1] + "/" + date[0];
			return date2;
		} else {
			return "";
		}
	}

}